package org.refresh.core.conf;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.refresh.core.Constraint;
import org.refresh.core.expr.BinaryExpression;
import org.refresh.core.expr.ConstantIntegerExpression;
import org.refresh.core.expr.Expression;
import org.refresh.core.expr.SourceVariableSumExpression;
import org.refresh.core.expr.SourceVariableValueExpression;
import org.refresh.core.expr.TargetVariableSumExpression;
import org.refresh.core.expr.TargetVariableValueExpression;

/*******************************************************************************
 * Copyright (c) 2005 Jules White. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Common
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
public abstract class AbstractExpressionCreatorHandler implements ConfigDirectiveHandler {

	public static final String TARGET_PREFIX = "Target.";
	public static final String SOURCE_PREFIX = "Source.";

	public static final String SUM_POSTFIX = ".Sum";

	private String OPS = "=<>-+!";

	public void handle(RefreshProblem problem, String context,
			String directive, String argstr) {
		StringTokenizer tk = new StringTokenizer(argstr.trim(),OPS,true);
		String[] rfeatures = new String[tk.countTokens()];
		int i = 0;
		while(tk.hasMoreTokens()){
			rfeatures[i]=tk.nextToken().trim();
			i++;
		}
		
		String op = "";
		Expression curr = null;
		for (String val : rfeatures) {
			if (OPS.indexOf(val) > -1) {
				op += val;
			} else {
				Expression expr = getExpression(val);
				if (curr != null) {
					curr = new BinaryExpression(curr, expr, op);
				} else {
					curr = expr;
				}
			}
		}

		handle(problem,context,directive,argstr,curr);
	}
	
	public abstract void handle(RefreshProblem problem, String context, String directive, String argstr, Expression expr);

	public Expression getExpression(String expr) {
		Expression cexpr = null;
		
		if (expr.startsWith(TARGET_PREFIX)) {
			if (expr.endsWith(SUM_POSTFIX)) {
				String var = expr.substring(TARGET_PREFIX.length(),expr.length()-SUM_POSTFIX.length());
				cexpr = new TargetVariableSumExpression(var);
			} else {
				String var = expr.substring(TARGET_PREFIX.length());
				cexpr = new TargetVariableValueExpression(var);
			}
		}
		else if(expr.startsWith(SOURCE_PREFIX)){
			boolean sum = false;
			if (expr.endsWith(SUM_POSTFIX)) {
				String var = expr.substring(SOURCE_PREFIX.length(),expr.length()-SUM_POSTFIX.length());
				cexpr = new SourceVariableSumExpression(var);
			} else {
				String var = expr;
				cexpr = new SourceVariableValueExpression(var.substring(SOURCE_PREFIX.length()));
			}
		}
		else{
			try{
				Integer i = Integer.parseInt(expr.trim());
				cexpr = new ConstantIntegerExpression(i);
			}catch (Exception e) {
				
			}
		}
		return cexpr;
	}

}
